package com.mrjy.share.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.ShareMemberInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.share.bo.IShareMemberBo;

@Controller
@RequestMapping("/share/shareMember")
public class ShareMemberController {
	
	@Resource
	private IShareMemberBo shareMemberBo;
	
	/**
	 * 后台统计所有一级总收入
	 * @param sa
	 * @return
	 */
	@RequestMapping("/listShareCommisionInfo")
	@ResponseBody
	public Object listShareCommisionInfo(ShareMemberInfo sa){		
		return shareMemberBo.listShareCommisionInfo(sa);
	}
	
	
	/**
	 * 统计当前一级分销用户总收入、推荐数、未结算、已结算数据
	 * @param sa
	 * @return
	 */
	@RequestMapping("/shareCommisionByUserId")
	@ResponseBody
	public Object shareCommisionByUserId(ShareMemberInfo sa,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == sa.getUserId()){
			sa.setUserId(userInfo.getId());
		}
		return shareMemberBo.shareCommisionByUserId(sa);
	}
	
	/**
	 * 查询分销详细记录，包括一级分销本身推广以及属下二级所有推广，全部，已结算，未结算
	 * @param sa
	 * @param session
	 * @return
	 */
	@RequestMapping("/listShareMembersByUserId")
	@ResponseBody
	public Object listShareMembersByUserId(ShareMemberInfo sa,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == sa.getUserId()){
			sa.setUserId(userInfo.getId());
		}
		return shareMemberBo.listShareMembersByUserId(sa);
	}
	
	/**
	 * 根据二级分销的shareCode查询其下成员：
	 * @param sa.shareCode
	 * @return
	 */
	@RequestMapping("/queryListBySharecode")
	@ResponseBody
	public Object queryListBySharecode(ShareMemberInfo sa){
		return shareMemberBo.queryListBySharecode(sa);
	}
	
	/**
	 * 根据userId获取
	 * @param userId
	 * @return
	 */
	@RequestMapping("/queryShareCodeByUserId")
	@ResponseBody
	public Object queryShareCodeByUserId(Integer userId){
		return shareMemberBo.queryShareCodeByUserId(userId);
	}
	
	/**
	 * 插入分享码,
	 * @param shareMemberInfo;share_code,user_id
	 * @param session
	 * @author Javin : isUse = true, isNew = true
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public Object insert(ShareMemberInfo shareMemberInfo,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		Map<String, Object> map = new HashMap<String, Object>();
		if(userInfo == null){
			map.put("err_code", 1);
			map.put("err_msg", "没有用户信息");
		}
		shareMemberInfo.setUserId(userInfo.getId());
		shareMemberBo.txInsert(shareMemberInfo,map);
		return map;
	}
	
	
	
}
